<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
$return['basic']=$basic;
set_html('title','发布作品');
if($_RQ['op']=='main'){
	//查询在进行的项目
	$rid = intval($_RQ['rid']);
	$isfabunums = pdo_count('jy_xiuvote_opus',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($basic['maxfabu'] >0 && ($isfabunums >= $basic['maxfabu'])){
		exi('已达总发布上限，不能再发布作品！','error','myopus/');
	}
	$post=array(
		//'title'=>'我“'.$member['nickname'].'”发起了人生脚印！欢迎大家来点赞，留下祝福的声音！',
		//'description'=>'或许青春的星光有些朦胧，但我们有足够的热情点亮自我的人生灯盏！为我加油！',
	);
	$post=array('isshow'=>1,'realname'=>$member['nickname'],'age'=>18,'sex'=>'1');
	if($rid){
		$reply=pdo_get('jy_xiuvote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$rid),array('id','starttime','endtime','title','fileds','showtype'));
		if($reply['starttime']<time() && $reply['endtime']>time()){
					$reply['status']=1;
		}elseif($reply['endtime']<time()){
					exi('该活动已结束！','error','index/');
		}else{
					exi('该活动还没开始！','error','index/');
		}
		$fileds = json_decode($reply['fileds'],true);
		foreach($fileds as &$item){
			$item['value']=explode(';',$item['value']);
		}
		$post['rid'] = $rid;
		$post['fileds'] = $fileds;
		$return['reply']=$reply;
	}else{
		$list=pdo_getall('jy_xiuvote_reply',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
		if($list){
			foreach($list as &$row){
				$row=$row['title'];
			}
		}
	}
	
	$member=pdo_get('jy_xiuvote_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	//判断是否是会员
	if($member['isvip']==1 && $member['viptime']>=time()){
		$member['ishongbao']=1;
	}else{
		$member['ishongbao']=0;
	}
	//是否关注
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$isfabunums = pdo_count('jy_xiuvote_opus',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$postdata['rid']));
		$reply = pdo_get('jy_xiuvote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['rid']),array('showtype','maxfabu','fabuzs','isfollow'));
		if(!$return['isfollow'] && ($reply['isfollow']=='2' || $reply['isfollow']=='3')){
			$return['needfollow']=1;
			exi($return);
		}
		if($reply['maxfabu'] >0 && ($isfabunums >= $reply['maxfabu'])){
			exi('已达该活动的发布上限，您可以参加其他活动！','error','myopus/');
		}
		if(!$postdata['title']){
			exi('作品标题不能为空！','error');
		}
		if(!$postdata['description']){
			exi('作品描述不能为空！','error');
		}
		if(!$postdata['opus_images'] && $postdata['type'] ==1){
			exi('作品图片不能为空！','error');
		}
		if(!$postdata['video'] && $postdata['type'] !=1){
			exi('作品视频不能为空！','error');
		}
		//判断字段必填
		if(!empty($postdata['fileds'])){
			foreach($postdata['fileds'] as $item){
				if($item['is_must'] && empty($item['data'])){
					exi('请填写:'.$item['title'],'error');
				}
			}
		}
		if($basic['needsh']){
			$status=0;
		}else{
			$status=1;
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'rid'=>$postdata['rid'],
			'title'=>$postdata['title'],
			'realname'=>$postdata['realname'],
			'age'=>$postdata['age'],
			'sex'=>$postdata['sexid']-1,
			'status'=>$status,
			'isshow'=>$postdata['isshow'],
			'thumb'=>$postdata['opus_images']['0']?:$postdata['thumb'],
			'description'=>$postdata['description'],
			'opus_images'=>ijson_encode($postdata['opus_images']),
			'fileddata'=>ijson_encode($postdata['fileds']),
			'video'=>$postdata['video'],
			'plugin'=>'jy_videoshop',
			'goodsid'=>$postdata['goods']['id'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_xiuvote_opus',$insert);
		$id=pdo_insertid();
		//参赛赠送(给参赛者)
		if($reply['fabuzs']){
			cfc('core')->member_credit($_SESSION['uid'],'credit1',$reply['fabuzs'],1,'jy_xiuvote','参赛赠送(给参赛者)');
		}
		exi('上传作品成功','success','jy_xiuvote/myopus/main');
	}
	$member=pdo_get('jy_xiuvote_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
	if((!$member['isvip'] || $member['viptime'] < time()) && $basic['isfee_hongbao']){
		$member['ishongbao'] =0;
	}else{
		$member['ishongbao'] =1;
	}
	$post['opus_images']=array();
	$post['type']=0;
	$return['params']=array('keyword'=>'');
	$return['post']=$post;
	$return['list']=$list;
	$return['member']=$member;
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['province']=cfc('tools')->pcd();
	$return['os']=$_SESSION['os'];
	exi($return);

}
if($_RQ['op']=='getfileds'){
	//查询自定义字段
	$fileds=pdo_getcolumn('jy_xiuvote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'fileds');
	$fileds = json_decode($fileds,true);
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	$return['post']=array();
	$return['post']['fileds']=$fileds;
	$return['success']=1;
	exi($return);
}